package com.ruoyi.web.controller.mall.goodsType.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.mall.goodsType.domain.GoodsType;
import com.ruoyi.web.controller.mall.goodsType.service.IGoodsTypeService;
import com.ruoyi.web.controller.mall.goodsTypeParams.domain.GoodsTypeParams;
import com.ruoyi.web.controller.mall.goodsTypeParams.service.IGoodsTypeParamsService;
import com.ruoyi.web.controller.mall.goodsTypeSpec.domain.GoodsTypeSpec;
import com.ruoyi.web.controller.mall.goodsTypeSpec.service.IGoodsTypeSpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品类型Controller
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/mall/goodsType")
public class GoodsTypeController extends BaseController
{

    @Autowired
    private IGoodsTypeService goodsTypeService;
    @Autowired
    private IGoodsTypeSpecService goodsTypeSpecService;
    @Autowired
    private IGoodsTypeParamsService goodsTypeParamsService;

    /**
     * 查询商品类型列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody GoodsType goodsType)
    {
        startPage();
        List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
        return getDataTable(list);
    }

    /**
     * 获取商品规格名详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        GoodsType goodsType = goodsTypeService.selectGoodsTypeById(id);
        List<GoodsTypeSpec> specList= goodsTypeSpecService.queryByTypeId(id);
        List<GoodsTypeParams> paramsList = goodsTypeParamsService.queryByTypeId(id);
        goodsType.setTypeSpecList(specList);
        goodsType.setTypeParamsList(paramsList);
        return AjaxResult.success(goodsType);
    }

    /**
     * 导出商品类型列表
     */
    @Log(title = "商品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody GoodsType goodsType)
    {
        List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
        ExcelUtil<GoodsType> util = new ExcelUtil<GoodsType>(GoodsType.class);
        return util.exportExcel(list, "goodsType");
    }

    /**
     * 新增保存商品类型
     */
//    @RequiresPermissions("mall:goodsType:add")
    @Log(title = "商品类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody GoodsType goodsType)
    {
        return toAjax(goodsTypeService.addGoodsType(goodsType));
    }

    /**
     * 修改保存商品类型
     */
//    @RequiresPermissions("mall:goodsType:edit")
    @Log(title = "商品类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody GoodsType goodsType)
    {
        return toAjax(goodsTypeService.editGoodsType(goodsType));
    }

    /**
     * 删除商品类型
     */
//    @RequiresPermissions("mall:goodsType:remove")
    @Log(title = "商品类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    public AjaxResult remove(@RequestBody GoodsType goodsType)
    {
        return toAjax(goodsTypeService.removeGoodsTypeById(goodsType.getId()));
    }
}
